"""
Case Type   : HTAP行列融合特性
Case Name   : 指定分区部分列转换后删除数据，验证查询数据正确
Create At   : 2025/1/24
Owner       : ningyali
Description :
    1.建表进行行列转换
    2.查询query_imcstore_views
    3.删除数据
    4.备机查询
    5.查询query_imcstore_views
    6.开启enable_imcsscan，explain和查询
    7.关闭enable_imcsscan，explain和查询
    8.清理环境
Expect      :
    1.执行成功
    2.执行成功
    3.执行成功
    4.执行成功
    5.执行成功，修改操作会优先存储在增量表中，等到满足条件后重新构建列存缓存，因此该视图数据可能不会更新
    6.执行成功, 查询计划走imcs scan
    7.执行成功，查询计划不走imcs scan,查询结果与第6步一致
    8.执行成功
History     :
"""

import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro


class HTAP(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta_sh = CommonSH('Standby1DbUser')
        self.Constant = Constant()
        self.common = Common()
        self.t_name = 't_htap_0139'

    def test_trans(self):
        text = '----step1:建表进行行列转换; expect:执行成功----'
        self.log.info(text)
        sql = f'drop table if exists {self.t_name};\n' \
              f'create table {self.t_name}(\n' \
              f'    c1 integer,\n' \
              f'    c2 varchar(30),\n' \
              f'    c3 char(30))\n' \
              f'partition by range (c1)(\n' \
              f'    partition {self.t_name}_p1 values less than (1000),\n' \
              f'    partition {self.t_name}_p2 values less than (2000),\n' \
              f'    partition {self.t_name}_p3 values less than (3000),\n' \
              f'    partition {self.t_name}_p4 values less than ' \
              f'(maxvalue));\n' \
              f'insert into {self.t_name} values (' \
              f'generate_series(1, 100000),' \
              f'generate_series(1, 100000)||\'aaa\',' \
              f'generate_series(1, 100000)||\'bbb\');' \
              f'alter table {self.t_name} modify partition ' \
              f'{self.t_name}_p4 imcstored(c1,c1,c3,c3,c3,c3,c3,c3);' \
              f'\\d+'
        sql_res = self.pri_sh.execut_db_sql(sql)
        expect_res = r'CREATE TABLE\s+INSERT 0 100000\s+ALTER TABLE'
        self.assertRegex(sql_res, expect_res, f'执行失败:{text}')

        text = '----step2:查询query_imcstore_views; expect:执行成功----'
        self.log.info(text)
        q_sql = f'select *,cu_num_in_mem+cu_num_in_disk as all_num ' \
                f'from query_imcstore_views() ' \
                f'where relname like \'{self.t_name}%\' order by relname;'
        pri_res1 = self.pri_sh.execut_db_sql(q_sql)
        sta_res1 = self.sta_sh.execut_db_sql(q_sql)
        sta_rows1, _ = self.common.parse_sql_query_res(sta_res1)
        cu_size_in_disk1 = int(sta_rows1[0][-3])
        cu_num_in_disk1 = int(sta_rows1[0][-2])

        text = '----step3:删除数据; expect:执行成功----'
        self.log.info(text)
        sql = f'delete from {self.t_name} where c1%3=0;' \
              f'vacuum {self.t_name};' \
              f'\\d+'
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.assertIn('DELETE 33333', sql_res, f'执行失败:{text}')

        text = '----step4:备机查询; expect:执行成功----'
        self.log.info(text)
        self.log.info('需要等待同步，因此用循环等待')
        sql = f'select count(c1) from {self.t_name};'
        for i in range(60):
            sql_res = self.sta_sh.execut_db_sql(sql)
            if '66667' in sql_res:
                break
            time.sleep(10)
        self.assertIn('66667', sql_res, f'执行失败:{text}')

        text = '----step5:查询query_imcstore_views; ' \
               'expect:执行成功，修改操作会优先存储在增量表中，等到满足条件后重新构建列存缓存，因此该视图数据可能不会更新----'
        self.log.info(text)
        pri_res2 = self.pri_sh.execut_db_sql(q_sql)
        sta_res2 = self.sta_sh.execut_db_sql(q_sql)
        sta_rows2, _ = self.common.parse_sql_query_res(sta_res2)
        cu_size_in_disk2 = int(sta_rows2[0][-3])
        cu_num_in_disk2 = int(sta_rows2[0][-2])
        self.log.info(f'cu_size_in_disk2:{cu_size_in_disk2}, '
                      f'cu_size_in_disk1:{cu_size_in_disk1}')
        self.log.info(f'cu_num_in_disk2:{cu_num_in_disk2}, '
                      f'cu_num_in_disk1:{cu_num_in_disk1}')
        self.assertEqual(pri_res1, pri_res2, f'执行失败:{text}')
        self.assertGreaterEqual(cu_size_in_disk2, cu_size_in_disk1,
                                f'执行失败:{text}')
        self.assertGreaterEqual(cu_num_in_disk2, cu_num_in_disk1,
                                f'执行失败:{text}')

        text = '----step6:开启enable_imcsscan，explain和查询; ' \
               'expect:执行成功, 查询计划走imcs scan----'
        self.log.info(text)
        q_sql = f'set enable_imcsscan=on;' \
                f'explain (costs false) select c3,c1,c1||c3 ' \
                f'from {self.t_name} partition ({self.t_name}_p4) ' \
                f'order by c1 limit 10;' \
                f'select c3,c1,c1||c3 from {self.t_name} ' \
                f'partition ({self.t_name}_p4) order by c1 limit 10;'
        sql_res = self.sta_sh.execut_db_sql(q_sql)
        rows1, _ = self.common.parse_sql_query_res(sql_res)
        expect = [['3001bbb', '3001', '30013001bbb'],
                  ['3002bbb', '3002', '30023002bbb'],
                  ['3004bbb', '3004', '30043004bbb'],
                  ['3005bbb', '3005', '30053005bbb'],
                  ['3007bbb', '3007', '30073007bbb'],
                  ['3008bbb', '3008', '30083008bbb'],
                  ['3010bbb', '3010', '30103010bbb'],
                  ['3011bbb', '3011', '30113011bbb'],
                  ['3013bbb', '3013', '30133013bbb'],
                  ['3014bbb', '3014', '30143014bbb']]
        self.assertIn('IMCStore Scan', sql_res, f'执行失败:{text}')
        self.assertEqual(expect, rows1[-10:], f'执行失败:{text}')

        text = '----step7:关闭enable_imcsscan，explain和查询; ' \
               'expect:执行成功，查询计划不走imcs scan,查询结果与第6步一致----'
        self.log.info(text)
        q_sql = q_sql.replace('enable_imcsscan=on', 'enable_imcsscan=off')
        sql_res = self.sta_sh.execut_db_sql(q_sql)
        rows2, _ = self.common.parse_sql_query_res(sql_res)
        self.assertNotIn('IMCStore Scan', sql_res, f'执行失败:{text}')
        self.assertEqual(rows1[-10:], rows2[-10:], f'执行失败:{text}')

    def tearDown(self):
        text = '----step8:清理环境;expect:执行成功----'
        self.log.info(text)
        sql = f'drop table if exists {self.t_name};'
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.assertIn('DROP TABLE', sql_res, f'删除表失败:{text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
